package cn.com.czpb.client.module.nanning.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.czpb.client.R;
import cn.com.czpb.client.app.FragmentVPAdapter;
import cn.com.czpb.client.model.news.AreaChannelBean;
import cn.com.czpb.client.module.nanning.fragment.NanningItemFragment;
import cn.com.czpb.client.module.news.NewsBaseFragment;

public class CityFragmentPagerAdapter extends FragmentVPAdapter<NewsBaseFragment> {
    private int selectedPosition = 0;
    private View view;
    TextView tv;
    Context context;

    public CityFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    public void setSelectedPosition(int mselectedPosition) {
        this.selectedPosition = mselectedPosition;
    }

    public void sortChannel(List<AreaChannelBean> saveTitleList) {
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment entry : fragments) {
            ft.remove(entry);
        }
        ft.commitAllowingStateLoss();
        ft = null;
        fm.executePendingTransactions();
        List<NewsBaseFragment> list = new ArrayList<NewsBaseFragment>();


        for (int i = 0; i < saveTitleList.size(); i++) {
            AreaChannelBean ccb = saveTitleList.get(i);
            NanningItemFragment fragment = NanningItemFragment.newInstance(ccb);
            fragment.setTitle(ccb.getName());
            list.add(fragment);
        }
        setFragments(list);
    }


    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public View getTabView(int position) {

        view = LayoutInflater.from(context).inflate(R.layout.tab_comment, null,false);
        tv = view.findViewById(R.id.comment_mine);
        NewsBaseFragment frag = fragments.get(position);
        String ti = frag.getTitle();
        tv.setText(ti);
        return view;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        NewsBaseFragment frag = fragments.get(position);

        String ti = frag.getTitle();
        return ti;
    }

}